﻿Imports FairShop.Objects
Imports System.Windows.Forms

Public MustInherit Class GrafiekAreaBase
    Inherits Control

    Const MIN_VERTICAL_SCALE_SPACING As Integer = 30
    Const MIN_HORIZONTAL_SCALE_SPACING As Integer = 120

    Protected _data As GrafiekData
    Protected _origin As Integer = 0
    Protected _verticalSpan As Double = 0

    Public Enum GrafiekType
        Pin
        Blok
        Lijn
    End Enum

#Region "Public en protected property's"

    Public Property Data() As GrafiekData
        Get
            Return _data
        End Get
        Set(ByVal value As GrafiekData)
            _data = value
            CalculateOrigin()
        End Set
    End Property

    Public ReadOnly Property Origin() As Integer
        Get
            Return _origin
        End Get
    End Property

    Public MustOverride ReadOnly Property Type() As GrafiekType

    Public ReadOnly Property GridLineIntervalVertical() As Double
        Get
            If (_data Is Nothing) Then Return 0
            If (_verticalSpan = 0) Then Return 0
            Return Me.Height * ValueIntervalPerGridline / _verticalSpan ' double om bij meervouden niet teveel af te wijken
        End Get
    End Property

    Public ReadOnly Property ValueIntervalPerGridline() As Double
        Get
            If (_data Is Nothing) Then Return 0
            Dim valueInterval As Double = _verticalSpan * MIN_VERTICAL_SCALE_SPACING / Me.Height
            Dim logSchaal As Integer = Convert.ToInt32(Math.Floor(Math.Log10(valueInterval)))
            valueInterval = (Math.Floor(valueInterval / 10 ^ logSchaal)) * 10 ^ logSchaal
            Return valueInterval
        End Get
    End Property

    Public ReadOnly Property GridLineIntervalHorizontal() As Integer
        Get
            If (_data Is Nothing) Then Return 0
            Return Convert.ToInt32(MonthsPerGridline * 30.5 * Me.Width / _data.HorizontalSpan)
        End Get
    End Property

    Public ReadOnly Property MonthsPerGridline() As Integer
        Get
            If (_data Is Nothing) Then Return 0
            Dim intervalInDays As Double = _data.HorizontalSpan * MIN_HORIZONTAL_SCALE_SPACING / Me.Width
            Return Convert.ToInt32(Math.Floor(intervalInDays / 30.5) + 1)
        End Get
    End Property

#End Region

    Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
        MyBase.OnResize(e)
        CalculateOrigin()
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        If (_data Is Nothing) Then Return
        ' draw scales
        ' vertical
        If (GridLineIntervalVertical > 0) Then
            For i As Integer = -Convert.ToInt32((Me.Height - Origin) / GridLineIntervalVertical) + 1 To Convert.ToInt32(Origin / GridLineIntervalVertical)
                Dim text As String = (i * ValueIntervalPerGridline).ToString()
                Dim h As Integer = Convert.ToInt32(Origin - i * GridLineIntervalVertical)
                e.Graphics.DrawLine(Pens.Gray, 0, h, Me.Width, h)
            Next
        End If
        ' horizontal
        If (GridLineIntervalHorizontal > 0) Then
            For i As Integer = 0 To Convert.ToInt32(Me.Width / GridLineIntervalHorizontal) + 1
                Dim x As Integer = i * GridLineIntervalHorizontal
                e.Graphics.DrawLine(Pens.Gray, x, 0, x, Me.Height)
            Next
        End If
    End Sub

    Private Sub CalculateOrigin()
        If (_data Is Nothing) Then Return
        Dim maxValue As Decimal = _data.MaxWaarde
        Dim minValue As Decimal = _data.MinWaarde

        _verticalSpan = maxValue - minValue
        If (_verticalSpan = 0) Then
            _origin = 0
        Else
            If (maxValue > 0 AndAlso minValue < 0) Then
                _origin = Convert.ToInt32(maxValue * Me.Height / _verticalSpan)
            Else
                _origin = Convert.ToInt32(Me.Height / 2)
            End If
        End If
    End Sub

End Class
